package priv.mk.jdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;


public class JDBCUtil {
	private static Logger logger = Logger.getLogger(JDBCUtil.class);

	public static Map<String, Object> findsingle(String sql, Object... args) {
		/*
		 * 查询单行信息
		 */

		Map<String, Object> map = new HashMap<>();
		query(sql, new QueryVisiter() {

			@Override
			public void handerset(ResultSet set) throws Exception {
				ResultSetMetaData metaData = set.getMetaData();// 获取列信息
				int columnCount = metaData.getColumnCount();// 获取列数
				for (int i = 1; i < columnCount; i++) {
					System.out.print(metaData.getColumnName(i) + "\t");// 列头
				}
				System.out.println();
				if (set.next()) {// 针对每一行进行迭代
					for (int i = 1; i < columnCount; i++) {

						map.put(metaData.getColumnName(i), set.getObject(i));
					}

				}

			}
		}, args);
		return map;
	}

	public static List<Map<String, Object>> findAll(String sql, Object... args) {
		/*
		 * 查询全部表信息
		 */
		final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		query(sql, new QueryVisiter() {

			@Override
			public void handerset(ResultSet set) throws Exception {
				ResultSetMetaData metaData = set.getMetaData();// 获取列信息
				int columnCount = metaData.getColumnCount();// 获取列数
				while (set.next()) {// 针对每一行进行迭代
					Map<String, Object> map = new HashMap<>();
					for (int i = 1; i <= columnCount; i++) {
						map.put(metaData.getColumnName(i), set.getObject(i));
					}
					list.add(map);
				}

			}
		}, args);
		return list;
	}

	public static boolean update(String sql, Object... args) {
		/*
		 * 更新表信息，DML操作
		 */
		int set = 0;
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		try {
			connection = DBUtil.getconn();// 1.通过工具获取连接
			connection.setAutoCommit(false);// 关闭自动提交
			prepareStatement = connection.prepareStatement(sql);
			for (int i = 1; i < args.length; i++) {
				prepareStatement.setObject(i, args[i - 1]);
			}
			set = prepareStatement.executeUpdate();// 4.执行SQL语句
			connection.commit();// 手动提交
			return set > 0;
		} catch (Exception e) {
			try {// 若抛出异常，则进行回滚
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {// 6.关闭资源
			DBUtil.close(null, connection, prepareStatement);
		}
		return false;
	}

	private static void query(String sql, QueryVisiter visiter, Object... args) {
		/*
		 * 为findall findsingle提供支持 
		 */
		ResultSet set = null;
		Connection connection = null;
		PreparedStatement prepareStatement = null; 
		try {
			connection = DBUtil.getconn();//获取连接
			prepareStatement = connection.prepareStatement(sql);//预编译SQL语句
			set = prepareStatement.executeQuery();//执行SQL语句
			visiter.handerset(set);
			// 5.处理结果集

		} catch (Exception e) {
			e.printStackTrace();
		}
		// 6.关闭资源
		finally {
			DBUtil.close(set, connection, prepareStatement);

		}
	}

}
